package com.novellotus.httploadingview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


/**
 * Created by：Rich on 2017/5/10 15:24
 * 网络加载封装布局
 */
public class LoadingView extends RelativeLayout implements View.OnClickListener {

    /**
     * 默认提示TextView
     */
    public String mWarnText;
    /**
     * 默认加载中TextView
     */
    private String mLoadingText;

    /**
     * 默认提示文本
     */
    public TextView mWarnView;
    /**
     * 默认重新加载btn
     */
    public Button mLoadDataBtn;
    /**
     * 需要绑定的View
     */
    private View mBindView;

    /**
     * 是否设置自定义加载view
     */
    private boolean hasCustomLoadingView = false;

    /**
     * 警告logo
     */
    private ImageView warnImage;

    private View mCustomLoadingView;

    private OnBtnClickListener onBtnClickListener;

    private LoadingDialog mProgressDialog;

    private int mLoadingModel = MODEL_DEFAULT;

    //默认模式
    public static final int MODEL_DEFAULT = 1;
    //弹出框模式
    public static final int MODEL_ALERT = 2;

    public LoadingView(Context context) {
        super(context);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);

    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    /**
     * 在自定义布局中添加控件
     */
    private void init(Context context, AttributeSet attrs) {
        //setBackgroundResource(R.drawable.shape_app_background);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingView, 0, 0);
        mWarnText = typedArray.getString(R.styleable.LoadingView_hh_empty_warn_txt);
        String buttonText = typedArray.getString(R.styleable.LoadingView_hh_empty_button_txt);
        mLoadingText = typedArray.getString(R.styleable.LoadingView_hh_empty_loading_txt);
        typedArray.recycle();

        if (TextUtils.isEmpty(mWarnText)) {
            mWarnText = "数据加载中";
        }

        if (TextUtils.isEmpty(buttonText)) {
            buttonText = "重新加载";
        }

        if (TextUtils.isEmpty(mLoadingText)) {
            mLoadingText = "加载中";
        }

        mLoadDataBtn = new Button(getContext());
        mLoadDataBtn.setText(buttonText);
        mLoadDataBtn.setTextSize(15);
        mLoadDataBtn.setTextColor(Color.parseColor("#ffffff"));
        mLoadDataBtn.setBackgroundResource(R.drawable.selector_button);
        LayoutParams mLoadingDataLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, (int) getContext().getResources().getDimension(R.dimen.button_height));
        mLoadingDataLp.addRule(RelativeLayout.CENTER_IN_PARENT);
        mLoadDataBtn.setId(R.id.id_hh_empty_btn_view);
        mLoadDataBtn.setVisibility(View.INVISIBLE);
        addView(mLoadDataBtn, mLoadingDataLp);

        mWarnView = new TextView(getContext());
        mWarnView.setText(mWarnText);
        mWarnView.setTextSize(17);
        mWarnView.setTextColor(Color.parseColor("#333333"));
        LayoutParams mWarnLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mWarnLp.setMargins(0, 0, 0, (int) getContext().getResources().getDimension(R.dimen.text_margin_button));
        mWarnLp.addRule(RelativeLayout.CENTER_IN_PARENT);
        mWarnLp.addRule(RelativeLayout.ABOVE, R.id.id_hh_empty_btn_view);
        mWarnView.setId(R.id.id_hh_empty_tv_view);
        addView(mWarnView, mWarnLp);

        warnImage = new ImageView(getContext());
        warnImage.setImageResource(R.drawable.icon_internel_tips);

        LayoutParams imgLp = new LayoutParams((int) getContext().getResources().getDimension(R.dimen.warn_image), (int) getContext().getResources().getDimension(R.dimen.warn_image));
        imgLp.addRule(RelativeLayout.ABOVE, R.id.id_hh_empty_tv_view);
        imgLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
        imgLp.setMargins(0, 0, 0, (int) getContext().getResources().getDimension(R.dimen.warn_image_margin));
        warnImage.setId(R.id.id_hh_warn_img);
        warnImage.setVisibility(View.INVISIBLE);
        addView(warnImage, imgLp);


        mProgressDialog = new LoadingDialog(getContext());
        mProgressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); //设置对话框背景透明 ，对于AlertDialog 就不管用了

        mLoadDataBtn.setOnClickListener(this);
        //  setVisibility(GONE);
    }

    /**
     * 加载中
     */
    public void loading() {
        warnImage.setVisibility(View.INVISIBLE);
        if (mBindView != null) {
            mBindView.setVisibility(GONE);
        }
        if (mLoadingModel == MODEL_DEFAULT) {
            setVisibility(VISIBLE);
            mLoadDataBtn.setVisibility(INVISIBLE);
            if (!hasCustomLoadingView) {
                mWarnView.setText("加载中");
            } else {
                if (mCustomLoadingView != null) {
                    mWarnView.setVisibility(GONE);
                    mCustomLoadingView.setVisibility(VISIBLE);
                }
            }
        } else if (mLoadingModel == MODEL_ALERT) {
            setVisibility(VISIBLE);
            mWarnView.setVisibility(View.VISIBLE);
            mWarnView.setText("加载中");
            mLoadDataBtn.setVisibility(INVISIBLE);
            mProgressDialog.setCancelable(true);
            mProgressDialog.show();
        }

    }

    /**
     * 加载成功
     */
    public void success() {

        if (mBindView != null) {
            mBindView.setVisibility(VISIBLE);
        }
        if (mLoadingModel == MODEL_ALERT) {
            mProgressDialog.dismiss();
        }
        setVisibility(GONE);


    }


    /**
     * 加载失败
     *
     * @param msg 加载失败提示语
     */
    public void empty(String msg, boolean button) {

        if (!TextUtils.isEmpty(msg)) {
            mWarnText = msg;
        }

        if (mLoadingModel == MODEL_ALERT) {
            mProgressDialog.cancel();
        }
        mProgressDialog.dismiss();
        if (button) {
            mLoadDataBtn.setVisibility(View.VISIBLE);
            warnImage.setVisibility(View.VISIBLE);
            warnImage.setImageResource(R.drawable.icon_internel_tips);
        } else {
            warnImage.setVisibility(View.VISIBLE);
            mLoadDataBtn.setVisibility(View.INVISIBLE);
            warnImage.setImageResource(R.drawable.icon_no_data);
        }

        mWarnView.setVisibility(View.VISIBLE);
        mWarnView.setText(mWarnText);
        setVisibility(View.VISIBLE);
    }




    /**
     * 设置绑定view
     *
     * @param view
     */
    public void bindView(View view) {
        this.mBindView = view;
    }

    public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
        this.onBtnClickListener = onBtnClickListener;
    }

    @Override
    public void onClick(View v) {
        if (onBtnClickListener != null) {
            onBtnClickListener.onReload();
        } else {
            //     throw new IllegalArgumentException("must be set click");
        }
    }

    /**
     * 设置自定义加载view
     *
     * @param view
     */
    public void setCustomLoadingView(View view) {
        if (view != null) {
            mCustomLoadingView = view;
            hasCustomLoadingView = true;

            mWarnView.setVisibility(GONE);

            LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            params.addRule(RelativeLayout.ABOVE, R.id.id_hh_empty_btn_view);
            addView(view, params);
            invalidate();
        }
    }

    /**
     * 设置弹出框模式加载中文字
     *
     * @param loadingText
     */
    public void setLoadingText(String loadingText) {
        if (!TextUtils.isEmpty(loadingText)) {
            if (mProgressDialog != null) {
                //mProgressDialog.setLoadingText(loadingText);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    public void setLoadingModel(int model) {
        this.mLoadingModel = model;
    }

    public interface OnBtnClickListener {
        /**
         * 重新加载回调接口
         */
        void onReload();
    }
}
